//
//  ZYModeSelectView.m
//  YiLin
//
//  Created by Zyark on 2018/6/4.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYModeSelectView.h"
#import "ZYModeTypeModel.h"
#import "UIButton+ImageTitleSpacing.h"

@interface ZYModeSelectView()
//@property(nonatomic,strong) NSMutableArray<UIImageView *> *imageViewArray;
//@property(nonatomic,strong) NSMutableArray<UILabel *> *labelArray;
@property(nonatomic,strong) NSMutableArray<UIView *> *bgViewArray;
@property(nonatomic,strong) NSMutableArray<UIButton *> *buttonArray;
@property(nonatomic,strong) NSArray<ZYModeTypeModel *> *dataArray;
@property(nonatomic,assign) BOOL isAlterSelectView;
@end

@implementation ZYModeSelectView

-(instancetype)initWithFrame:(CGRect)frame modelArray:(NSArray<ZYModeTypeModel *> *)modelArray{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWithModelArray:modelArray];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame deviceType:(ZYDeviceType)deviceType{
    self = [super initWithFrame:frame];
    if (self) {
        if(deviceType == ZYDeviceTypeVentilationSystem){
            [self setupVSModeSelectView];
        }else{
            [self setupCommonModeSelectView];
        }
    }
    return self;
}

-(void)setupCommonModeSelectView{
    NSArray<NSNumber *> *modeTypeArray = @[[NSNumber numberWithUnsignedInteger:ZYDeviceModeEnergySaving],
                                           [NSNumber numberWithUnsignedInteger:ZYDeviceModeComfortable],
                                           [NSNumber numberWithUnsignedInteger:ZYDeviceModeCustom]];
    NSArray<NSString *> *imageNameArray = @[@"ic_home_mode_energy_saving_unselected",
                                            @"ic_home_mode_comfortable_unselected",
                                            @"ic_home_mode_custom_unselected"];
    NSArray<NSString *> *selectImageNameArray = @[@"ic_home_mode_energy_saving_selected",
                                                  @"ic_home_mode_comfortable_selected",
                                                  @"ic_home_mode_custom_selected"];
    NSArray<NSString *> *titleArray = @[LC_DEVICE_MODE_TYPE_ENERGY_SAVING,
                                        LC_DEVICE_MODE_TYPE_COMFORTABLE,
                                        LC_DEVICE_MODE_TYPE_CUSTOM];
    NSMutableArray *array = [NSMutableArray array];
    for (NSUInteger i=0; i<3; i++) {
        ZYModeTypeModel *model = [[ZYModeTypeModel alloc] init];
        model.modeType = [[modeTypeArray objectAtIndex:i] unsignedIntegerValue];
        model.imageName = [imageNameArray objectAtIndex:i];
        model.selectImageName = [selectImageNameArray objectAtIndex:i];
        model.title = [titleArray objectAtIndex:i];
        [array addObject:model];
    }
    [self setupWithModelArray:array];
}

-(void)setupVSModeSelectView{
    NSArray<NSNumber *> *modeTypeArray = @[[NSNumber numberWithUnsignedInteger:ZYVSModeTypeCommon],
                                           [NSNumber numberWithUnsignedInteger:ZYVSModeTypePurification],
                                           [NSNumber numberWithUnsignedInteger:ZYVSModeTypeSleeping],
                                           [NSNumber numberWithUnsignedInteger:ZYVSModeTypeMixedWind],
                                           [NSNumber numberWithUnsignedInteger:ZYVSModeTypeAuto]];
    NSArray<NSString *> *imageNameArray = @[@"ic_vs_mode_type_common_unselected",
                                            @"ic_vs_mode_type_purification_unselected",
                                            @"ic_vs_mode_type_sleeping_unselected",
                                            @"ic_vs_mode_type_mixed_wind_unselected",
                                            @"ic_vs_mode_type_auto_unselected"];
    NSArray<NSString *> *selectImageNameArray = @[@"ic_vs_mode_type_common_selected",
                                            @"ic_vs_mode_type_purification_selected",
                                            @"ic_vs_mode_type_sleeping_selected",
                                            @"ic_vs_mode_type_mixed_wind_selected",
                                            @"ic_vs_mode_type_auto_selected"];
    NSArray<NSString *> *titleArray = @[LC_VS_MODE_TYPE_COMMON,
                                        LC_VS_MODE_TYPE_PURIFICATION,
                                        LC_VS_MODE_TYPE_SLEEPING,
                                        LC_VS_MODE_TYPE_MIXED_WIND,
                                        LC_VS_MODE_TYPE_AUTO];
    NSMutableArray *array = [NSMutableArray array];
    for (NSUInteger i=0; i<5; i++) {
        ZYModeTypeModel *model = [[ZYModeTypeModel alloc] init];
        model.modeType = [[modeTypeArray objectAtIndex:i] unsignedIntegerValue];
        model.imageName = [imageNameArray objectAtIndex:i];
        model.selectImageName = [selectImageNameArray objectAtIndex:i];
        model.title = [titleArray objectAtIndex:i];
        [array addObject:model];
    }
    [self setupWithModelArray:array];
}


-(void)setupWithModelArray:(NSArray<ZYModeTypeModel *> *)modelArray{
    _dataArray = modelArray;
    _isAlterSelectView = NO;
    int count = 0;
    NSUInteger total = [modelArray count];
    CGFloat itemHeight = self.width * 0.32;
    CGFloat height = ceilf((float)total / 3) * itemHeight;
    _bgViewArray = [NSMutableArray array];
    _buttonArray = [NSMutableArray array];
    self.height = height;
    _realHeight = height;
    for (ZYModeTypeModel *mode in modelArray) {
        int column = count %  3;//列
        int row = floor(count / 3);//行
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake((self.width / 3) * column, row * itemHeight, self.width / 3, itemHeight)];
        [bgView setUserInteractionEnabled:YES];
        [self addSubview:bgView];
        [_bgViewArray addObject:bgView];
        UIButton *button = [self setupButtonWithMode:mode size:CGSizeMake(bgView.width, bgView.width)];
        [bgView addSubview:button];
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
        [_buttonArray addObject:button];
        button.centerX = bgView.width / 2;
        button.centerY = bgView.height /2;
//        button.imageEdgeInsets = UIEdgeInsetsMake(0,button.width /2,0,-button.width /2);
//        UIImageView *imageView = [self setupImageView:mode.imageName];
//        [bgView addSubview:imageView];
//        UILabel *label = [self setupTitleLabel];
//        [bgView addSubview:label];
//        imageView.centerX = bgView.width / 2;
//        imageView.centerY = bgView.height /2 - label.height;
//        label.centerX = imageView.centerX;
//        label.top = imageView.bottom + 10;
        count ++;
    }
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    self.alpha = 0;
}

-(UIButton *)setupButtonWithMode:(ZYModeTypeModel *)mode size:(CGSize)size{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    [button setTarget:self action:@selector(doSlectMode:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = mode.modeType;
    [button setImage:[UIImage imageNamed:mode.imageName] forState:UIControlStateNormal];
    [button setTitleColor:kMainColor forState:UIControlStateNormal];
    [button setTitleColor:kMainColor forState:UIControlStateHighlighted];
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateNormal];
    [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateHighlighted];
    [button.titleLabel setFont:[UIFont systemFontOfSize:11]];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    return button;
}

#pragma mark - Event
-(void)doSlectMode:(UIButton *)button{
    [self showOrHidden];
    if([_delegate respondsToSelector:@selector(modeSelectView:setModeType:)]){
        [_delegate modeSelectView:self setModeType:button.tag];
    }
}

#pragma mark - public

-(void)setupTitle{
    NSUInteger count = 0;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    for (ZYModeTypeModel *mode in _dataArray) {
        UIButton *button = [_buttonArray objectAtIndex:count];
        [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateNormal];
        [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateHighlighted];
        count ++;
    }
}

-(void)showOrHidden{
    _isAlterSelectView = !_isAlterSelectView;
    ZYWeakSelf(weakSelf)
    [UIView animateWithDuration:0.3 animations:^{
        if(weakSelf.isAlterSelectView){
            weakSelf.alpha = 1;
        }else{
            weakSelf.alpha = 0;
        }
    }];
}

-(void)close{
    _isAlterSelectView = NO;
    ZYWeakSelf(weakSelf)
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.alpha = 0;
    }];
}

@end
